#!/usr/bin/env python
# -*- coding: utf-8 -*-
import base64
import hashlib
import random, string

__author__ = 'ethereal'
__mtime__ = '2018/8/26'


# 生成加密密码
# 传递明文密码
class UserService():
    # 产生授权码
    # 参数是传递用户对象
    @staticmethod
    def geneAuthCode(user_info):
        m = hashlib.md5()
        str = "%s-%s-%s-%s" % (user_info.uid, user_info.login_name, user_info.login_pwd, user_info.login_salt)
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    @staticmethod
    def genePwd(pwd, salt):
        m = hashlib.md5()
        str = "%s-%s" % (base64.encodebytes(pwd.encode('utf-8')), salt)
        m.update(str.encode("utf-8"))
        return m.hexdigest()

    #最终生成结果 类似'eHDw9c4SVjwqYpco'
    @staticmethod
    def geneSalt(length=16):
        keylist = [random.choice((string.ascii_letters + string.digits)) for i in range(length)]
        return ("".join(keylist))
